import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/models/evaluation/evaluationBenchmarkReportResult.dart';
import 'package:ichinesereader/pages/teacher/evaluationReportPdf.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:pdf/pdf.dart';
import 'package:printing/printing.dart';
import 'package:path_provider/path_provider.dart';

class EvaluationReportPreview extends StatelessWidget {
  EvaluationReportPreview(
      {Key? key, required this.reportData, required this.level})
      : super(key: key);

  final BenchmarkReportData reportData;
  final int level;

  @override
  Widget build(BuildContext context) {
    Future<void> _saveAsFile(
      BuildContext context,
      LayoutCallback build,
      PdfPageFormat pageFormat,
    ) async {
      final bytes = await build(pageFormat);

      final appDocDir = await getApplicationDocumentsDirectory();
      final appDocPath = appDocDir.path;
      final file = File('$appDocPath/document.pdf');
      print('Save as file ${file.path} ...');
      await file.writeAsBytes(bytes);
      //  await OpenFile.open(file.path);
    }

    final actions = <PdfPreviewAction>[
      if (!GetPlatform.isWeb)
        PdfPreviewAction(
          icon: const Icon(Icons.save),
          onPressed: _saveAsFile,
        )
    ];
    return Scaffold(
        appBar: CustomAppBar(title: 'Benchmark Report'.tr),
        body: PdfPreview(
          maxPageWidth: 700,
          build: (format) => generatePdf(format, reportData, level),
          actions: actions,
        ));
  }
}
